import TaskStateEnum from '@model/enum/TaskStateEnum.ts';
import Filter from '@src/filter/filter.js';
import RenderCell from 'packages/BaseTableCell/renderCell.js';
import formatColumn from 'packages/BaseTableCell/formatColumn';
import { isUndefined } from '@src/util/type'
import { isCalendar } from '@src/util/CalendarUtil';
import { Inquire } from '../../list/TaskFieldModel'
import i18n from '@src/locales'
import { 
  isNotSupportedDisplayField, 
  isSystemFiled, 
} from '@service/FieldService'

const inquireMap = {}
Inquire.forEach(field => {
  field.show = true; 
  inquireMap[field.fieldName] = field; 
})


// 高级搜索
export const getAdvancedFields = (vm) => {
  const that = vm
  const customerField = that.customerField || {}
  const customerOption = customerField?.setting?.customerOption || {}
  const systemFieldSetting = that.systemFieldSetting || {}
  // const isCalendar = JSON.parse(localStorage.getItem('isCalendar'))

  const fields = [
    // {
    //   displayName: '工单类型',
    //   fieldName: 'taskType',
    //   formType: 'select',
    //   setting: {
    //     isMulti: false,
    //     dataSource: that.taskTypeList
    //   },
    // },
    {
      fieldName: 'customer',
      displayName: i18n.t('common.base.customer'),
      formType: 'text',
    },
    {
      fieldName: 'tlmName',
      displayName: i18n.t('common.base.contact'),
      formType: 'select',
      show: customerOption.linkman
    },
    {
      fieldName: 'area',
      exportAlias: 'customerAddress',
      displayName: i18n.t('task.list.displayName.area'),
      formType: 'address',
      show: customerOption.address
    },
    {
      fieldName: 'cusAddress',
      displayName: i18n.t('task.list.displayName.detailAddress'),
      formType: 'text',
      show: customerOption.address
    },
    {
      fieldName: 'product',
      displayName: i18n.t('common.base.product'),
      formType: 'text',
      show: customerOption.product
    },
    {
      fieldName: 'serviceType',
      displayName: i18n.t('common.form.type.serviceType'),
      formType: 'select',
      show: systemFieldSetting.hasServiceType,
      setting: {
        dataSource: that.serviceTypeField?.setting?.dataSource || []
      }
    },
    {
      fieldName: 'serviceContent',
      displayName: i18n.t('common.form.type.serviceContent'),
      formType: 'select',
      show: systemFieldSetting.hasServiceContent,
      setting: {
        dataSource: that.serviceContentField?.setting?.dataSource || []
      }
    },
    {
      fieldName: 'level',
      displayName: i18n.t('common.form.type.level'),
      formType: 'select',
      show: systemFieldSetting.hasLevel,
      setting: {
        dataSource: that.levelField?.setting?.dataSource || []
      }
    },
    {
      fieldName: 'description',
      displayName: i18n.t('common.form.type.description'),
      formType: 'description',
      show: systemFieldSetting.hasDescription
    },
    inquireMap.oncePrinted,
    {
      fieldName: 'createUser',
      displayName: i18n.t('common.base.createUser'),
      formType: 'user',
      isSystem: 1,
      defineType: []
    },
    {
      isSystem: 1,
      fieldName: 'createTime',
      displayName: i18n.t('common.base.createTime'),
      formType: 'datetime',
      orderId: 1,
    },
    {
      fieldName: 'updateTime',
      displayName: i18n.t('task.list.displayName.updateTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'planTime',
      displayName: i18n.t('common.form.type.planTime'),
      formType: 'datetime',
      show: !isCalendar
    },
    {
      fieldName: 'planStartTime',
      displayName: i18n.t('common.form.type.planStartTime'),
      formType: 'datetime',
      show: isCalendar
    },
    {
      fieldName: 'planEndTime',
      displayName: i18n.t('common.form.type.planEndTime'),
      formType: 'datetime',
      show: isCalendar
    },
    inquireMap.inApprove,
  ]

  return fields.map(item => {
    return {
      ...item,
      setting: item.setting || {},
      defaultValue: null,
      show: item.show ?? 1,
      isSearch: item.isSearch ?? 1,
      isNull: item.isNull ?? 1,
      isSystem: item.isSystem ?? 1,
      orderId: item.orderId ?? 0,
    }
  });
}


// 列表 && 选择列
export const getColumnFields = (vm) => {
  const that = vm;
  const customerField = that.customerField || {}
  const customerOption = customerField?.setting?.customerOption || {}
  // const isCalendar = JSON.parse(localStorage.getItem('isCalendar'))

  const fields = [
    {
      fieldName: 'taskNo',
      displayName: i18n.t('common.form.type.taskNo'),
      formType: 'text',
      renderCell: (h, col, row) => {
        return RenderCell.renderTask.call(that, h, row, that.allowTaskViewDetail);
      }
    },
    {
      displayName: i18n.t('common.task.taskType'),
      fieldName: 'taskType',
      formType: 'select',
      renderCell: (h, col, row) => {
        return (<div>{row.templateName || ''}</div>)
      }
    },
    {
      fieldName: 'customer',
      displayName: i18n.t('common.base.customer'),
      formType: 'text',
      renderCell: (h, col, row) => {
        let displayName = row['customerEntity'] && row['customerEntity'].name;
        let customerId = row?.customerEntity?.id;
        return RenderCell.renderCustomer.call(that, h, displayName, customerId, row.linkAuth)
      }
    },
    {
      fieldName: 'tlmName',
      displayName: i18n.t('common.base.contact'),
      formType: 'select',
      show: customerOption.linkman,
      visibleForSelectColumn: customerOption.linkman,
      renderCell: (h, col, row) => {
        return (<div>
          { row['linkMan'] && row['linkMan'].name }
        </div>)
      }
    },
    {
      fieldName: 'tlmPhone',
      displayName: i18n.t('common.base.phone'),
      formType: 'select',
      show: customerOption.linkman,
      visibleForSelectColumn: customerOption.linkman,
      renderCell: (h, col, row) => {
        return (<div>
          { row['linkMan'] && row['linkMan'].phone }
        </div>)
      }
    },
    {
      fieldName: 'area',
      displayName: i18n.t('common.form.type.customerAddress'),
      formType: 'address',
      show: customerOption.address,
      visibleForSelectColumn: customerOption.address,
      renderCell: (h, col, row) => {
        return (<div>{Filter.prettyAddress(row[col.formType])}</div>)
      }
    },
    {
      fieldName: 'product',
      displayName: i18n.t('common.base.product'),
      formType: 'text',
      show: customerOption.product,
      visibleForSelectColumn: customerOption.product,
    },
    {
      fieldName: 'serviceType',
      displayName: i18n.t('common.form.type.serviceType'),
      formType: 'select',
    },
    {
      fieldName: 'serviceContent',
      displayName: i18n.t('common.form.type.serviceContent'),
      formType: 'select',
    },
    {
      fieldName: 'level',
      displayName: i18n.t('common.form.type.level'),
      formType: 'select',
    },
    {
      fieldName: 'description',
      displayName: i18n.t('common.form.type.description'),
      formType: 'text'
    },
    {
      fieldName: 'createUserName',
      displayName: i18n.t('common.base.createUser'),
      formType: 'user',
      isSystem: 1,
    },
    {
      fieldName: 'createTime',
      displayName: i18n.t('common.base.createTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'state',
      displayName: i18n.t('task.taskState'),
      formType: 'text',
      renderCell: (h, col, row) => {
        let displayName = row[col.field] && TaskStateEnum.getName(row[col.field]);
        return RenderCell.renderTaskState(h, displayName, row[col.field]);
      }
    },
    {
      fieldName: 'planTime',
      displayName: i18n.t('common.form.type.planTime'),
      formType: 'datetime',
      show: !isCalendar,
      visibleForSelectColumn: !isCalendar,
    },
    {
      fieldName: 'planStartTime',
      displayName: i18n.t('common.form.type.planStartTime'),
      formType: 'datetime',
      show: isCalendar,
      visibleForSelectColumn: isCalendar,
    },
    {
      fieldName: 'planEndTime',
      displayName: i18n.t('common.form.type.planEndTime'),
      formType: 'datetime',
      show: isCalendar,
      visibleForSelectColumn: isCalendar,
    },
    {
      fieldName: 'updateTime',
      displayName: i18n.t('task.list.displayName.updateTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'oncePrinted',
      displayName: i18n.t('task.list.displayName.oncePrinted'),
      formType: 'select',
      renderCell: (h, col, row) => {
        return RenderCell.renderBoolean(h, row[col.field]);
      }
    },
    // {
    //   fieldName: 'inApprove',
    //   displayName: '审批状态',
    //   formType: 'select',
    //   renderCell: (h, col, row) => {
    //     return RenderCell.renderApproveStatus(h, row[col.field]);
    //   }
    // },
    {
      fieldName: 'allotOperation',
      displayName: i18n.t('common.base.operation'),
      formType: 'text',
      fixed: 'right',
      width: '88px',
      show: 'important'
    },
  ]

  return fields.map(item => {
    // 初始化属性
    let field = {
      ...item,
      setting: item.setting ?? {},
      isSystem: item.isSystem ?? 1,
      isNull: item.isNull ?? 1,
      isSearch: item.isSearch ?? 1,
      orderId: item.orderId ?? 1,
      show: item.show ?? true,
      visibleForSelectColumn: item.visibleForSelectColumn ?? true,
      templateId: item.templateId,
    };
    
    return formatColumn(field); 
  })
};
